package com.ballyza;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@SuppressWarnings("serial")
public class OpenFileServlet extends HttpServlet {

	@Override
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {
			ServletFileUpload upload = new ServletFileUpload();
			resp.setContentType("text/plain");
			
			FileItemIterator iterator = upload.getItemIterator(req);
			while(iterator.hasNext()) {
				FileItemStream item = iterator.next();
				InputStream stream = item.openStream();
				
				int len;
				byte[] buffer = new byte[8192];
				while((len = stream.read(buffer, 0, buffer.length)) != -1) {
					resp.getOutputStream().write(buffer, 0, len);
				}
			}
			
		} catch(Exception e) {
			
		}
	}
	
}